<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 任务平台管理模块
 */
class taskplat extends Admin_Controller {
	
    function __construct()
    {
        parent::__construct();
        $this->load->model('taskplat_model');
        $this->load->model('account_model');
        $this->load->model('users_log_model');
        $this->load->model('finance_list_model');
        $this->load->model('../../../Application/models/task_model');
    }
    //任务列表
    public function index()
    {
		//分页
        $where = "1 = 1";
        $st = trim($_GET['st']);
        $key = trim($_GET['key']);
        if ($key != '') {
            if ($st == 1) {
                $con = '发布人';
                $where .= " and u.username like '%$key%'";
            }
            if ($st == 2) {
                $con = '任务名称';
                $where .= " and t.title like '%$key%'";
            }
        }
        if (trim($_GET['time-start'])) {
            $time_start = strtotime($_GET['time-start']);
            $where .= " and t.add_time >= $time_start";

        }

        if (trim($_GET['time-end'])) {
            $time_end = strtotime($_GET['time-end']) + 3600 * 24;
            $where .= "and t.add_time <= $time_end";

        }

        $getcount = $this->db->query("select t.* from tan_task t left join tan_users u on u.user_id=t.uid where $where order by t.tid desc")->num_rows();
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $query_param = "t.*,u.username";
        $list = $this->db->query("select $query_param from tan_task t left join tan_users u on u.user_id=t.uid where $where order by t.tid desc limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();
        foreach ($list as $key=>$val){
            $list[$key]['add_time'] = date('Y-m-d H:i:s',$val['add_time']);
            $list[$key]['subcount'] = $this->db->query("select * from tan_subtasks where tid=".$val['tid'])->num_rows();
            $list[$key]['status_val'] = "";
            if ($val['status']==1) $list[$key]['status_val'] .= "<font color='#4dab0e'>审核通过</font>";
            if ($val['status']==2) $list[$key]['status_val'] .= "<font color='#f00'>审核不通过</font>";
            if ($val['status']==0) $list[$key]['status_val'] .= "<font color='#f0a'>未支付</font>";
            if ($val['status']==3) $list[$key]['status_val'] .= "<font color='#00f'>已支付</font>";

            if ($val['is_end']==0) $list[$key]['status_val'] .= "&nbsp;&nbsp;<font color='#4dab0e'>未结束</font>";
            if ($val['is_end']==1) $list[$key]['status_val'] .= "&nbsp;&nbsp;<font color='#00f'>已结束</font>";

            $list[$key]['detail_url'] = 'http://'.$_SERVER['HTTP_HOST']."/task/".encrypt_wenda_id($val['tid']).".html";
        }
        $this->template->assign('stname', $st);
        $this->template->assign('keyname', trim($_GET['key']));
        $this->template->assign('conname', $con);
        $this->template->assign('time_start', trim($_GET['time-start']));
        $this->template->assign('time_end', trim($_GET['time-end']));
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        $this->template->load('taskplat/index');
    }

    //任务审核
    public function taskstatus(){
        if($_GET){
            $mssage = array();
            $tid	   = trim($_GET['tid']);
            $data['status'] = trim($_GET['status']);

            $this->db->trans_begin();//开启事务
            if ($data['status']==2){
                $task_info = $this->db->query("select * from tan_task where tid=$tid")->row_array();
                if ($task_info['status']==3){
                    $amount = $task_info['commission']*$task_info['sub_num']+$task_info['deposit']+$task_info['sponsorship_money'];
                    $account = $this->account_model->get($task_info['uid']);
                    $update_account = array(
                        'balance' => $account['balance'] + $amount,//账户余额
                        'expend' => $account['expend'] - $amount,//总额金额
                    );
                    $res1 = $this->account_model->update($update_account,$task_info['uid']);

                    //弹溜溜明细
                    $finance_list_data = array();
                    $finance_list_data['user_id'] = $task_info['uid'];
                    $finance_list_data['content'] = '任务：'.$task_info['title'].'，审核不通过，退回'.$amount.'溜溜币';
                    $finance_list_data['finance'] = $amount;
                    $finance_list_data['finance_balance'] = $update_account['balance'];
                    $finance_list_data['reward_type'] = 'spread_award';
                    $finance_list_data['type'] = 1;//收入
                    $finance_list_data['create_time'] = time();
                    $finance_list_data['month'] = date("Ym");
                    $finance_list_data['day'] = date("Ymd");
                    $res2 = $this->finance_list_model->insert($finance_list_data);
                }else{
                    $res1 = $res2 = true;
                }
            }else{
                $res1 = $res2 = true;
            }
            $result = $result = $this->db->where(array('tid'=>$tid))->update("tan_task",$data);
            $log_content =  $this->task_model->log_content();
            if($data['status'] == 1){
                //任务审核成功
                $content = $log_content['task_success'];
            }elseif($data['status'] == 2){
                //任务审核失败
                $content = $log_content['task_fail'];
            }
            if($result && $res1 && $res2){
                $this->db->trans_commit();//提交事务
                $mssage['info']	  = '操作成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                //添加日志

                $log_arr = array(
                    'type_id'=>$tid,
                    'uid'=>$this->user_info['user_id'],
                    'type' => 1,
                    'content'=>$content,
                    'add_time'=>time(),
                );
                $this->task_model->add_subtasks_log($log_arr);
                echo json_encode($mssage);exit;
            }else{
                $this->db->trans_rollback();//事务回滚
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }
        }
    }
    //任务
    public function subtask(){
        $task_id = $_GET['tid'] ? intval($_GET['tid']) : 0;
        if ($task_id){
            //分页
            $where = "st.tid = $task_id";
            $st = trim($_GET['st']);
            $key = trim($_GET['key']);
            if ($key != '') {
                if ($st == 1) {
                    $con = '任务领取人';
                    $where .= " and u.username like '%$key%'";
                }
                if ($st == 2) {
                    $con = '任务名称';
                    $where .= " and t.title like '%$key%'";
                }
            }
            if (trim($_GET['time-start'])) {
                $time_start = strtotime($_GET['time-start']);
                $where .= " and st.receive_time >= $time_start";

            }

            if (trim($_GET['time-end'])) {
                $time_end = strtotime($_GET['time-end']) + 3600 * 24;
                $where .= "and st.receive_time <= $time_end";

            }

            $getcount = $this->db->query("select st.* from tan_subtasks st left join tan_task t on t.tid=st.tid left join tan_users u on u.user_id=st.uid where $where order by st.tid desc")->num_rows();
            $navigation_position = 'outside';
            $records_per_page = 10;
            $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
            //$this->xpage->reverse(true);
            $this->xpage->records($getcount);
            $this->xpage->records_per_page($records_per_page);
            $this->xpage->labels("<<", ">>");
            $query_param = "st.*,t.title,u.username as claim,(select username from tan_users fu where fu.user_id = t.uid) as publisher ";
            $list = $this->db->query("select $query_param from tan_subtasks st left join tan_task t on t.tid=st.tid left join tan_users u on u.user_id=st.uid where $where order by t.tid desc limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();
            foreach ($list as $key=>$val){
                $list[$key]['receive_time'] = date('Y-m-d H:i:s',$val['receive_time']);
                $list[$key]['check_time'] = date('Y-m-d H:i:s',$val['check_time']);
                $list[$key]['add_time'] = date('Y-m-d H:i:s',$val['add_time']);
                $appeal_val = $this->db->query("select cid from tan_complaint where sid=".$val['sid'])->row_array();
                $list[$key]['appeal'] = $appeal_val['cid'];
            }
            $this->template->assign('stname', $st);
            $this->template->assign('keyname', trim($_GET['key']));
            $this->template->assign('conname', $con);
            $this->template->assign('time_start', trim($_GET['time-start']));
            $this->template->assign('time_end', trim($_GET['time-end']));
            $this->template->assign('Total',$getcount);
            $this->template->assign('showpage',$this->xpage->render(true));
            $this->template->assign('list',$list);
        }

        $this->template->load('taskplat/subtask');
    }
    //申诉管理
    public function appeal(){
        //分页
        $where = "1 = 1";
        $st = trim($_GET['st']);
        $key = trim($_GET['key']);
        if ($key != '') {
            if ($st == 1) {
                $con = '发起人';
                $where .= " and u.username like '%$key%'";
            }
            if ($st == 2) {
                $con = '任务名称';
                $where .= " and t.title like '%$key%'";
            }
        }
        if (trim($_GET['time-start'])) {
            $time_start = strtotime($_GET['time-start']);
            $where .= " and c.add_time >= $time_start";

        }

        if (trim($_GET['time-end'])) {
            $time_end = strtotime($_GET['time-end']) + 3600 * 24;
            $where .= "and c.add_time <= $time_end";

        }

        $getcount = $this->db->query("select c.* from tan_complaint c left join tan_subtasks s on s.sid=c.sid left join tan_users u on u.user_id=c.uid where $where order by c.cid desc")->num_rows();
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);u
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $query_param = "c.*,s.tid as task_id,u.username as initiator";
        $list = $this->db->query("select $query_param from tan_complaint c left join tan_subtasks s on s.sid=c.sid left join tan_users u on u.user_id=c.uid where $where order by c.cid desc limit ".(($this->xpage->get_page() - 1) * $records_per_page).",".$records_per_page)->result_array();
        foreach ($list as $key=>$val){
            $list[$key]['add_time'] = date('Y-m-d H:i:s',$val['add_time']);
            $list[$key]['kefu_time'] = $val['kefu_time']?date('Y-m-d H:i:s',$val['kefu_time']):'';
            $task_info = $this->db->query("select fu.username as publisher,t.title from tan_task t left join tan_users fu on t.uid=fu.user_id where t.tid=".$val['task_id'])->row_array();

            $list[$key]['publisher'] = $task_info['publisher'];
            $list[$key]['title'] = $task_info['title'];
            switch ($val['status']){
                case 0:
                    $list[$key]['status_val'] = "<font color='#00f'>申诉中</font>";
                    break;
                case 1:
                    $list[$key]['status_val'] = "<font color='#f0f'>发布方拒绝申诉</font>";
                    break;
                case 2:
                    $list[$key]['status_val'] = "<font color='#4dab0e'>申诉成功</font>";
                    break;
                case 4:
                    $list[$key]['status_val'] = "<font color='#00f'>取消申诉</font>";
                    break;
                case 5:
                    $list[$key]['status_val'] = "<font color='#f00'>申诉失败</font>";
                    break;
            }
            if ($val['is_kefu']==1){
                $list[$key]['is_kefu'] = "<font color='#f0f'>申请介入</font>";
            }else if ($val['is_kefu']==2){
                $list[$key]['is_kefu'] = "<font color='#00f'>已介入</font>";
            }else if ($val['is_kefu']==3){
                $list[$key]['is_kefu'] = "<font color='#4dab0e'>处理完成</font>";
            }else{
                $list[$key]['is_kefu'] = "";
            }
        }
        $this->template->assign('stname', $st);
        $this->template->assign('keyname', trim($_GET['key']));
        $this->template->assign('conname', $con);
        $this->template->assign('time_start', trim($_GET['time-start']));
        $this->template->assign('time_end', trim($_GET['time-end']));
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        $this->template->load('taskplat/appeal');
    }

    //申诉详情（客服介入）
    public function appeal_views(){
        if($_POST){
            $cid = trim($_POST['cid']);
            $data = array();
            $data['content_word'] = trim($_POST['content_word']);
            $data['cid'] = trim($_POST['cid']);
            $data['uid'] = 0;
            $data['add_time'] = time();
            if ($data['content_word']==''){
                $mssage['info']	  = '回复内容不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            $result = $this->db->insert("tan_complaint_comm",$data);
            if($result){
                $mssage['info']	  = '回复成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('taskplat/appeal_views').'?id='.$cid;
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('taskplat/appeal_views').'?id='.$cid;
                echo json_encode($mssage);exit;
            }

        }
        $cid = $_GET['id']?intval(trim($_GET['id'])):0;
        if ($cid){
            $appeal_info = $this->db->query("select c.*,u.username as initiator from tan_complaint c left join tan_users u on u.user_id=c.uid where c.cid=$cid")->row_array();
            $subtask_info =  $this->db->query("select * from tan_subtasks where sid=".$appeal_info['sid'])->row_array();
            $task_info =  $this->db->query("select t.*,u.username as publisher from tan_task t left join tan_users u on u.user_id=t.uid where t.tid=".$subtask_info['tid'])->row_array();
            $comm_info =  $this->db->query("select cc.*,u.username from tan_complaint_comm cc left join tan_users u on u.user_id=cc.uid where cc.cid=".$appeal_info['cid'])->result_array();

            $task_info['detail_url'] = 'http://'.$_SERVER['HTTP_HOST']."/task/".$task_info['tid'].".html";

            $subtask_info['add_time'] = $subtask_info["add_time"]?date("Y-m-d H:i:s",$subtask_info["add_time"]):'';
            $subtask_info['receive_time'] = $subtask_info["receive_time"]?date("Y-m-d H:i:s",$subtask_info["receive_time"]):'';
            $subtask_info['check_time'] = $subtask_info["check_time"]?date("Y-m-d H:i:s",$subtask_info["check_time"]):'';

            switch ($appeal_info['status']){
                case 0:
                    $appeal_info['status_val'] = "<font color='#00f'>申诉中</font>";
                    break;
                case 1:
                    $appeal_info['status_val'] = "<font color='#f0f'>发布方拒绝申诉</font>";
                    break;
                case 2:
                    $appeal_info['status_val'] = "<font color='#4dab0e'>申诉成功</font>";
                    break;
                case 4:
                    $appeal_info['status_val'] = "<font color='#00f'>取消申诉</font>";
                    break;
                case 5:
                    $appeal_info['status_val'] = "<font color='#f00'>申诉失败</font>";
                    break;
            }

            foreach ($comm_info as $item=>$value){
                if ($value['username']==''){
                    $comm_info[$item]['username'] = "<font color='#4dab0e'>客服</font>";
                }
                if ($value['uid']==$appeal_info['uid']){
                    $comm_info[$item]['username'] = "<font color='#f00'>".$value['username']."</font>";
                }
                if ($value['uid']==$task_info['uid']){
                    $comm_info[$item]['username'] = "<font color='#00f'>".$value['username']."</font>";
                }
                $comm_info[$item]['add_time'] = $value["add_time"]?date("Y-m-d H:i:s",$value["add_time"]):'';
            }
            $this->template->assign('appeal_info',$appeal_info);
            $this->template->assign('subtask_info',$subtask_info);
            $this->template->assign('task_info',$task_info);
            $this->template->assign('comm_info',$comm_info);
            $this->template->load('taskplat/appeal_views');
        }
    }
    //客服介入处理结果
    public function appealstatus(){
        if($_GET){
            $mssage = array();
            $cid	   = trim($_GET['cid']);
            $data['status'] = trim($_GET['status']);
            $data['is_kefu'] = 2;
            $this->db->trans_begin();//开启事务
            $result = $result = $this->db->where(array('cid'=>$cid))->update("tan_complaint",$data);

            if($result){
                $complaint = $this->db->query("select * from tan_complaint where cid=$cid")->row_array();
                if ($data['status']==2){
                    $subdata['status'] = 1;
                    $subdata['is_end'] = 1;
                    $subdata['is_gived'] = 1;

                    $subtask = $this->db->query("select st.*, t.commission from tan_subtasks st left join tan_task t on t.tid=st.tid where st.sid=".$complaint['sid'])->row_array();

                    if ($subtask['is_gived']==0){
                        $account = $this->account_model->get($subtask['uid']);
                        $amount = $subtask['commission'];

                        $update_account = array(
                            'balance' => $account['balance'] + $amount,//账户余额
                            'total' => $account['total'] + $amount,//总额金额
                        );
                        $res1 = $this->account_model->update($update_account,$subtask['uid']);

                        //弹溜溜明细
                        $finance_list_data = array();
                        $finance_list_data['user_id'] = $subtask['uid'];
                        $finance_list_data['content'] = '任务佣金'.$amount.'溜溜币';
                        $finance_list_data['finance'] = $amount;
                        $finance_list_data['finance_balance'] = $update_account['balance'];
                        $finance_list_data['reward_type'] = 'spread_award';
                        $finance_list_data['type'] = 1;//收入
                        $finance_list_data['create_time'] = time();
                        $finance_list_data['month'] = date("Ym");
                        $finance_list_data['day'] = date("Ymd");
                        $res2 = $this->finance_list_model->insert($finance_list_data);
                    }else{
                        $res1 = $res2 = true;
                    }
                    $res0 = $this->db->where(array('sid'=>$complaint['sid']))->update('tan_subtasks',$subdata);
                }
                if ($data['status']==5){
                    $subdata['status'] = 2;
                    $subdata['is_end'] = 2;
                    $res0 = $this->db->where(array('sid'=>$complaint['sid']))->update('tan_subtasks',$subdata);
                    $res1=$res2=true;
                }
                if ($res0 && $res1 && $res2){
                    $this->db->trans_commit();//提交事务
                    $mssage['info']	  = '操作成功';
                    $mssage['status'] = 1;
                    $mssage['url']	  = site_url('taskplat/appeal_views').'?id='.$cid;
                    echo json_encode($mssage);exit;
                }else{
                    $this->db->trans_rollback();//事务回滚
                    $mssage['info']	  = '操作失败';
                    $mssage['status'] = 0;
                    $mssage['url']	  = site_url('taskplat/appeal_views').'?id='.$cid;
                    echo json_encode($mssage);exit;
                }

            }else{
                $this->db->trans_begin();//开启事务
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('taskplat/appeal_views').'?id='.$cid;
                echo json_encode($mssage);exit;
            }
        }
    }
    /* 扣除押金 --lht 2018-01-31 */
    public function deduct_deposit(){
        if($_GET){
            $mssage = array();
            $tid	   = trim($_GET['tid']);
            $data['deposit'] = 0;
            $this->db->trans_begin();//开启事务
            $res1 = $result = $this->db->where(array('tid'=>$tid))->update("tan_task",$data);
            $log_data = array(
                'tid'       => $tid,
                'user_id'   => 0,
                'username'  => "管理员:" . $this->userinfo['username'],
                'content'   => "扣除押金",
                'add_time'  => time(),
            );

            if($res1){
                $this->db->trans_commit();//提交事务
                $mssage['info']	  = '操作成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $this->db->trans_rollback();//事务回滚
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }
        }
    }

    public function task_excel(){
        $tid = $_GET['tid'];
        $ListRowData = $this->task_model->task_reveive_record($tid);
        $task = $this->task_model->task_detail($tid);
        $this->load->library('Export');
        $HeadData = array('记录编号','标题','用户名','领取人','姓名','手机号','身份证','提交时间','领取时间','审核状态');
        $filename = $task['title'].'--'.date('Y-m-d');
        echo $this->export->users($HeadData,$ListRowData,$filename);exit;
    }
    //更新任务的已完成数量
    public function ajax_update_task_num(){
        $sub_num = isset($_REQUEST['sub_num']) ? trim($_REQUEST['sub_num']) : 0;
        $tid = $_REQUEST['tid'];
        $sid = $_REQUEST['sid'];
        //如果数量大于之前的任务数量，那么任务结束
        $task_info = $subtask_info = $this->db->query("select * from tan_task where tid={$tid}")->row_array();
        $this->db->query("update tan_subtasks set is_end=1 where sid={$sid}");
        if($sub_num < $task_info['sub_num']){
            //任务结束
            $dif_num = $task_info['sub_num'] - $sub_num;
            $real_num = $sub_num;
            $this->db->query("update tan_task set sub_num=$dif_num");
            //给佣金
        }else{
            $this->db->query("update tan_task set is_end=1}");
            //给佣金
            $real_num = $task_info['sub_num'];
        }
        //更新subtask 的end_num
        $this->db->query("update tan_subtasks set end_num=$real_num where sid=$sid");
        $subtask = $this->db->query("select st.*, t.commission from tan_subtasks st left join tan_task t on t.tid=st.tid where st.sid=$sid")->row_array();

        if ($subtask['is_gived']==0){
            $account = $this->account_model->get($subtask['uid']);
            $amount = $subtask['commission'] * $real_num;

            $update_account = array(
                'balance' => $account['balance'] + $amount,//账户余额
                'total' => $account['total'] + $amount,//总额金额
            );
            $res3 = $this->account_model->update($update_account,$subtask['uid']);

            //弹溜溜明细
            $finance_list_data = array();
            $finance_list_data['user_id'] = $subtask['uid'];
            $finance_list_data['content'] = '任务佣金'.$amount.'溜溜币';
            $finance_list_data['finance'] = $amount;
            $finance_list_data['finance_balance'] = $update_account['balance'];
            $finance_list_data['reward_type'] = 'spread_award';
            $finance_list_data['type'] = 1;//收入
            $finance_list_data['create_time'] = time();
            $finance_list_data['month'] = date("Ym");
            $finance_list_data['day'] = date("Ymd");

            $res4 = $this->finance_list_model->insert($finance_list_data);
        }else{
            $res3 = $res4 = true;
        }

        $subdata['status'] = 1;
        $subdata['is_end'] = 1;
        $subdata['is_gived'] = 1;
        $res1 = $this->db->where(array('sid'=>$sid))->update('tan_subtasks',$subdata);
        $mssage['info']	  = '操作成功';
        $mssage['status'] = 0;
        $mssage['url']	  = $_SERVER['HTTP_REFERER'];
        echo json_encode($mssage);exit;
    }
    //审核子任务
    public function ajax_judge_subtask(){
        $sid = $_REQUEST['sid'];
        $qualification =  isset($_REQUEST['qualification']) ? trim($_REQUEST['qualification']) : 0;
        $this->db->query("update tan_subtasks set qualification={$qualification} where sid={$sid}");
        $mssage['info']	  = '操作成功';
        $mssage['status'] = 0;
        $mssage['url']	  = $_SERVER['HTTP_REFERER'];
        echo json_encode($mssage);exit;
    }
    public function B2Blist(){
        $is_end =  isset($_REQUEST['is_end']) ? trim($_REQUEST['is_end']) : 0;
        $pageIndex = isset($_REQUEST['pageIndex']) ? trim($_REQUEST['pageIndex']) : 0;
        $where = " t.cate_id=4 ";
        if($is_end > 0){
            $where .= " t.is_end={$is_end} ";
        }
        $records_per_page = 12;
        $totalCount = $this->task_model->task_count($where);
        $sql = "select count(*) as num from tan_subtasks s left join tan_task t on t.tid=s.tid where t.cate_id=4";
        $getcount = $this->db->query($sql)->num_rows();

        $sql_limit = "select st.*,t.title,t.sub_num,t.commission,u.mobile,u.username receiptor,(select username from tan_users u where u.user_id = t.uid) as username  from tan_subtasks st left join tan_task t on t.tid=st.tid left join tan_users u on u.user_id=st.uid where t.cate_id=4 limit $pageIndex,$records_per_page";
        $list = $this->db->query($sql_limit)->result_array();
        foreach($list as $key=>$val){
            if($val['qualification']==0){
                $status_val = '待审核';
            }else if($val['qualification']==1){
                $status_val = '审核通过';
                if($val['apply_end']==1){
                    $status_val = '申请结束中';
                }
                if($val['is_end']==1){
                    $status_val = '任务结束';
                }
            }elseif($val['qualification']==2){
                $status_val = '审核不通过';
            }
            $list[$key]['status_val'] = $status_val;
            $list[$key]['commission'] = $val['commission']/10000;
            $list[$key]['add_time'] = date('Y-m-d H:i:s',$list[$key]['add_time']);
        }

        $this->template->assign('Total',$getcount);
        $this->template->assign('list', $list);
        $this->template->load('taskplat/bblist');
    }
}
